/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.service.util;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

/**
 * <p>Loads mail templates by specified identintifier from persistence storage</p>
 * <p><a href="MailTemplateLoader.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.6 $ $Date: 2006/08/03 10:07:45 $
 */
public class MailTemplateLoader extends ClasspathResourceLoader {

	protected transient final Log log = LogFactory.getLog(MailTemplateLoader.class);
    protected String encoding;

	/**
	 * Creates new instance of MailTemplateLoader
	 */
	public MailTemplateLoader() {
	}

	/**
	 * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)
	 */
	public void init(ExtendedProperties extendedProperties) {
		super.init(extendedProperties);
		encoding = rsvc.getString("content.encoding", "utf-8");

	}

	/**
	 * Returns template as a stream.<br />
	 * If input string starts with &quot;mailTemplate:&nbsp;&quot;, it will be returned as is, but prefix will be truncated.<br />
	 * Otherwise resource will be loaded from classpath
	 *
	 * @param templateName Name of resource to load
	 * @return Template as a stream
	 * @throws ResourceNotFoundException if specified resource could not be found
	 */
	public InputStream getResourceStream(String templateName) throws ResourceNotFoundException {

		InputStream result = null;

		if ( templateName.startsWith("mailTemplate: ") ) {
			templateName = templateName.substring("mailTemplate: ".length());
			byte[] template = null;
			try {
				template = templateName.getBytes(encoding);
			} catch ( UnsupportedEncodingException e ) {
				// do nothing
			}
			if ( template != null ) {
				result = new ByteArrayInputStream(template);
			}
		} else {
			result = super.getResourceStream(templateName);
		}

		return result;
	}

	/**
	 * Defaults to return false.
	 *
	 * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
	 */
	public boolean isSourceModified(Resource resource) {
		return super.isSourceModified(resource);
	}

	/**
	 * Defaults to return 0
	 *
	 * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
	 */
	public long getLastModified(Resource resource) {
		return super.getLastModified(resource);
	}
}
